//
//  MNCenterTreeFragment.swift
//  aj
//
//  Created by leeqb on 1/17/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class MNCenterTreeFragment: AJBaseViewController {
    var topLeftFamilyData: NSDictionary!
    @IBOutlet weak var centerFamily: FamilyHeartCenterView!
    @IBOutlet weak var topLeftFamily: FamilyHeartSideView!
    @IBOutlet weak var topRightFamily: FamilyHeartSideView!
    @IBOutlet weak var bottomLeftFamily: FamilyHeartSideView!
    @IBOutlet weak var bottomRightFamily: FamilyHeartSideView!
    @IBOutlet weak var bottomCenterFamily: FamilyHeartSideView!

    var familyList: [[NSObject : AnyObject]]! {
        didSet {
            self.familyMapping()
            /*if topLeftFamily1 != nil {
                topLeftFamily1.familyData = familyList[0]
            }*/
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    private func familyMapping() {
        for ele in self.familyList {
            let userList = ele["userList"] as? [[NSObject : AnyObject]]
            if userList?.count > 0 {
                for userEle in userList! {
                    let callName = userEle["callName"] as? String
                    
                    if callName == TITLE_ME || callName == TITLE_WIFE || callName == TITLE_BABY {
                        setCenterFamily(ele)
                        break
                    }
                    
                    if callName == TITLE_FATHER || callName == TITLE_MOTHER {
                        setTopLeft(ele)
                        break
                    }
                    
                    if callName == TITLE_WIFE_MOTHER || callName == TITLE_WIFE_FATHER {
                        setTopRight(ele)
                        break
                    }
                }
            }
        }
    }
    
    // 左上角桃心（爸爸妈妈）
    private func setTopLeft(familyData: [NSObject : AnyObject]) {
        if topLeftFamily != nil {
            topLeftFamily.familyData = familyData
        }
    }
    
    // 右上角桃心（爸爸妈妈）
    private func setTopRight(familyData: [NSObject : AnyObject]) {
        if topRightFamily != nil {
            topRightFamily.familyData = familyData
        }
    }
    
    // 中间桃心（我和妻子）
    private func setCenterFamily(familyData: [NSObject : AnyObject]) {
        if centerFamily != nil {
            centerFamily.familyData = familyData
        }
    }
    
    // 查看空间（本人、他人）
    @IBAction func viewSpace(sender: UITapGestureRecognizer) {
        var familyData: [NSObject : AnyObject]?
        if sender.view?.tag == 1 {
            let centerView = sender.view as! FamilyHeartCenterView
            familyData = centerView.familyData
        } else {
            let sideView = sender.view as! FamilyHeartSideView
            familyData = sideView.familyData
        }
        
        if familyData != nil {
            let vc = LB("Space", vcName: "LookSpace") as! SPCLookSpaceViewController
            vc.userId = String(userId)
            vc.familyId = familyData!["familyId"] as! String
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
